<?php

$_POST = $_DPOST;
if(!$_POST) exit('fail');
$bank = 'chinapay';
$PAY = PCache::read('pay.php');
if(!$PAY[$bank]['enable']) exit('fail');
if(!$PAY[$bank]['partnerid']) exit('fail');
//if(!$PAY[$bank]['keycode']) exit('fail');
require DT_INC.'/module.func.php';
$receive_url = '';
function log_result($word) {
	log_write($word, 'nchinapay');
}
require DT_ROOT."/api/pay/chinapay/netpayclient_config.php";
//加载 netpayclient 组件
require DT_ROOT."/api/pay/chinapay/netpayclient.php";
//导入公钥文件
$flag = buildKey(PUB_KEY);
$flag or exit('导入公钥文件失败！');

$merid        = $_REQUEST["merid"];         // 商户号
$orderno      = $_REQUEST["orderno"];       // 订单号
$transdate    = $_REQUEST["transdate"];     // 订单日期
$amount       = $_REQUEST["amount"];        // 订单金额
$currencycode = $_REQUEST["currencycode"];  // 货币代码
$transtype    = $_REQUEST["transtype"];     // 交易类型
$status       = $_REQUEST["status"];        // 交易状态
$checkvalue   = $_REQUEST["checkvalue"];    // 签名值
$gateId       = $_REQUEST["GateId"];        // 网关号
$priv1        = $_REQUEST["Priv1"];         // 备注
$flag = verifyTransResponse($merid, $orderno, $amount, $currencycode, $transdate, $transtype, $status, $checkvalue);
if($flag) {
	if($status == '1001') {
		//您的处理逻辑请写在这里，如更新数据库等。
		//注意：如果您在提交时同时填写了页面返回地址和后台返回地址，且地址相同，请在这里先做一次数据库查询判断订单状态，以防止重复处理该笔订单
		$do = new OwnCardModule();
		$owncardAction = Db('owncard');
		$buyinfo = $owncardAction->getByitem($priv1);
		log_write($priv1, 'itemid');
		log_write($buyinfo['username'], 'username');
		if($buyinfo) {
			if($buyinfo['status'] == 1) {
				$charge_orderid = $buyinfo['itemid'];
				$charge_money = $buyinfo['money'];
				$charge_amount = $buyinfo['money'];
				if($amount == padstr($charge_money*100, 12)) {
					$buyinfo['status']  = 2;
					$buyinfo['buytime'] = $DT_TIME;
					$owncardAction->updateBuycard($buyinfo);
					if($buyinfo['xdmoney']){
						money_add($buyinfo['username'], -$buyinfo['xdmoney']);
						money_record($buyinfo['username'], -$buyinfo['xdmoney'], $PAY[$bank]['name'], 'system', '车主金卡购买', "订单号：$charge_orderid;");
					}
					send_message($buyinfo['username'],"您的车主金卡订单已确认收到！","您已成功订购车主金卡(".$buyinfo['buynum'].")张，我们的客服将尽快与您联系发货！");
					exit('success');
				} else {
					$buyinfo['status']  = 4;
					$owncardAction->updateBuycard($buyinfo);
					exit('fail');
				}

			} else {
				exit('fail');
			}
		} else {
			log_result('通知订单号不存在R:'.$priv1);
			exit('fail');
		}
	}
}
exit('fail');
?>